<?php

namespace app\model;

use support\Db;
use support\Model;

class WaAdmins
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_admins';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;


    /**
     * 管理员列表 -分页
     * @return false|string
     */
    public static function list($page, $pageNum, $fields = ['*'], $condition = [], $between = [])
    {
        $offset = ($page - 1) * $pageNum;
        if ($between) {
            $model = Db::table('wa_admins as admin')
                ->leftJoin('wa_admin_roles as relevance', 'admin.id', '=', 'relevance.admin_id')
                ->leftJoin('wa_roles as roles', 'roles.id', '=', 'relevance.role_id')
                ->select($fields)
                ->where($condition)
//                ->where(['admin.status', '!=', 4])
                ->whereBetween('admin.created_at', $between);
        } else {
            $model = Db::table('wa_admins as admin')
                ->leftJoin('wa_admin_roles as relevance', 'admin.id', '=', 'relevance.admin_id')
                ->leftJoin('wa_roles as roles', 'roles.id', '=', 'relevance.role_id')
                ->select($fields)
                ->where($condition);
//                ->where(['admin.status', '!=', 4]);
        }

        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('admin.id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);
        // 当前用的 部门 角色     创建人的 昵称 部门 角色
        if ($list) {
            foreach ($list as $k) {
                $admin_data = self::adminDepartmentRole($k->id); // 管理员id
                $admin_pid_data = self::adminDepartmentRole($k->created_pid); // 创建人id
                if ($k->merchandiser_id) {
                    $admins = WaAdmins::find(['id' => $k->merchandiser_id]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $merchandiser = $admins->nickname;
                    } else {
                        $merchandiser = '';
                    }

                } else {
                    $merchandiser = '';
                }
                $roles = json_decode($k->role_id);
                $roles_name = [];
                if ($roles) {
                    foreach ($roles as $ke) {
                        $role_name = WaRoles::find(['id' => $ke]);
                        $role_name = json_decode($role_name);

                        if ($role_name) {
                            array_push($roles_name, $role_name->name);
                        }
                    }
                }
                $k->role_id = json_decode($k->role_id);
                $k->roles_name = $roles_name;
                $k->merchandiser = $merchandiser;
                $k->admin_department = $admin_data['department'];
                $k->admin_role = $admin_data['role'];
                $k->admin_created_name = $admin_pid_data['nickname'];
                $k->admin_created_department = $admin_pid_data['department'];

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);
    }

    /**
     * 管理员列表 - 无分页
     * @param $page
     * @param $pageNum
     * @param string[] $fields
     * @param array $condition
     * @return false|string
     */
    public static function all($fields, $condition)
    {
        $list = Db::table('wa_admins')
            ->select($fields)
            ->where($condition)
            ->orderBy('id', 'asc')
            ->get();
//        ->paginate($pageNum);
        // 当前用的 部门 角色     创建人的 昵称 部门 角色
        if ($list) {
            foreach ($list as $k) {
                $admin_data = self::adminDepartmentRole($k->id); // 管理员id
                $admin_pid_data = self::adminDepartmentRole($k->created_pid); // 创建人id
                $roles = json_decode($k->role_id);
                $roles_name = [];
                if ($roles) {
                    foreach ($roles as $ke) {
                        $role_name = WaRoles::find(['id' => $ke]);
                        $role_name = json_decode($role_name);

                        if ($role_name) {
                            array_push($roles_name, $role_name->name);
                        }
                    }
                }
                $k->roles_name = $roles_name;
                $k->admin_created_name = $admin_pid_data['username'];
                $k->admin_created_department = $admin_pid_data['department'];
//                $k->admin_department_data = $admin_department_data;
//                $k->admin_role_data = $admin_role_data;
                $k->admin_department = $admin_data['department'];
                $k->admin_role = $admin_data['role'];
            }
        }
        return json_encode($list);
    }


    public static function allId($fields, $condition)
    {

            $list = Db::table('wa_admins')
                ->select($fields)
                ->where($condition)
                ->get();
        return json_encode($list);
    }

    public static function allIdByWhereIn($fields, $condition,$whereIn)
    {

        if ($whereIn){
            $list = Db::table('wa_admins')
                ->select($fields)
                ->where($condition)
                ->whereIn('department_id',$whereIn)
                ->get();
        }else{
            $list = Db::table('wa_admins')
                ->select($fields)
                ->where($condition)
                ->get();
        }

        return json_encode($list);
    }


    // 通过管理员id 对应的 部门 角色 和 昵称
    public static function adminDepartmentRole($admin_id)
    {
        $data = [
            'role' => '',
            'department' => '',
            'username' => '',
            'nickname' => '',
        ];
        // 获取当前用户的 部门
        $admin = Db::table('wa_admins as admin')
            ->leftJoin('wa_department as department', 'admin.department_id', '=', 'department.id')
            ->select('admin.*', 'department.name as department_name')
            ->where('admin.id', $admin_id)
            ->first();
//        $admin = json_decode($admin);
        if ($admin) {
            $data['username'] = $admin->username;
            $data['nickname'] = $admin->nickname;
            $data['department'] = $admin->department_name;
            $role_name = [];
            if ($admin->role_id) {
                foreach (json_decode($admin->role_id) as $k) {
                    $roles = WaRoles::find(['id' => $k]);
                    $roles = json_decode($roles);
                    if ($roles) {
                        array($role_name, $roles->name);
                    } else {
                        $role_name = [];
                    }
                }
            }
            $data['role'] = $role_name;
        }

        return $data;
    }


    public static function userCount($start_time, $end_time)
    {

        $start_time = date('Y-m-d H:i:s', $start_time);
        $end_time = date('Y-m-d H:i:s', $end_time);
        $user_count = Db::table('wa_admins')
            ->select('*')
            ->whereBetween('created_at', [$start_time, $end_time])
            ->count(); // 用户数量
        return $user_count;

    }

    public static function find($condition = [])
    {
        $res = Db::table('wa_admins')
            ->where($condition)
            ->first();
        return json_encode($res);
    }

    public static function created($data)
    {

        return Db::table('wa_admins')->insertGetId($data);

    }

    public static function updated($condition, $data)
    {


        return Db::table('wa_admins')->where($condition)->update($data);

    }

}